<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Sub type data source
 * 
 * Returns aggregated resources where the RDF type is the same type or a sub type of the query entity.
 */
class escape_entityresolution_datasource_SubType extends escape_entityresolution_datasource_Aggregation
{
	/**
	 * Get a block (of entities) matching the entity specified.
	 * @param entityresolution_Entity $entity
	 * @param entityresolution_EntityFactory $entityFactory
	 * @return entityresolution_Block $block
	 */
	public function getBlock($entity, $entityFactory)
	{
		$block = new entityresolution_Block();
		
		$rdfParentTypes = $entity->getSourceData()->getResourceBySubject($entity->getSubjectUri())->getRdfTypes();

		$rdfTypes = null;
		if($rdfParentTypes !== null)
		{
			$rdfTypes = array();
			foreach($rdfParentTypes as $rdfParentType)
			{
				$rdfTypes[] = $rdfParentType;
				$subClassUriList = $this->repository->repositorySchema->getSubClassUriList($rdfParentType);
				foreach($subClassUriList as $subClassUri)
				{
					$rdfTypes[] = $subClassUri;
				}
			}	
			$rdfTypes = array_unique($rdfTypes);			
		}
		
		$aggregatedResources = $this->aggregation->getAggregatedResources($rdfTypes);
		
		foreach($aggregatedResources as $aggregatedResource)
		{
			/* @var $aggregatedResource escape_repository_OreAggregatedResource */
			
			$rdfResource = $aggregatedResource->getProperties();
			$rdfDocument = simplerdf_RdfSchemaDocument::createRdfDocument();
			$rdfDocument->addResource($rdfResource);
			
			$entity = $entityFactory->createEntity($rdfDocument, $rdfResource->getSubject());
					
			$blockEntry = new entityresolution_BlockEntry();
			$blockEntry->setEntity($entity);
			
			$block->addEntry($blockEntry);
		}
		
		return $block;
	}
	
}